<?php
/**
 * Created by PhpStorm.
 * User: admin
 * Date: 2018/5/4
 * Time: 17:06
 */

namespace app\admin\controller\sunfc;
use app\admin\controller\Base;
use think\Db;


class Publish extends Base
{

    // 信息列表信息
    public function index()
    {
        if (request()->isAjax()) {
            $res = Db::name('dd_publish')->where('is_del',0)->select();
            foreach ($res as $index => $re) {
                $res[$index]['create_time'] = date('Y-m-d H:i:s', $re['create_time']);
                $res[$index]['depart_time'] = date('Y-m-d H:i:s', $re['depart_time']);
                //json对象要在这里进行转换
            }
            $this->success('ok', '', $res);
        }
        else {
            $count = Db::name('dd_publish')->where('is_del',0)->count();
            $this->assign('count', $count);
            return $this->fetch();
        }

    }


    // 信息单独删除一条数据
    public function del(){
        $id = input('id');
        if($id){
            Db::name('dd_publish')-> where('id',$id)-> update(['is_del'  => '1']);
            $this -> success('操作成功');
        }
        else{
            $this -> error('操作失败');
        }
    }


    // 信息对数据进行批量删
    public function pidel()
    {
        $data = input('');
        $ids = $data['ids'];
        if (!$ids) {
            $this->error('异常');
        }
        Db::name('dd_publish')->where('id','in',$ids)-> update(['is_del'  => '1']);
        $this->success('操作成功');
    }


    // 信息发布进行搜索查询
    public function cases_search()
    {
        $status[0] = '禁用';
        $status[1] = '正常';
        $page       = input('page') ? input('page') : 1;
        $limit      = input('limit') ? input('limit') : 10;

        // 检索条件 -- 开始结束时间
        $s_time = strtotime(trim(input("depart_time",'')));
        $e_time = strtotime(trim(input("end_time",'')));

        $where = [];
        $where["is_del"] = 0;

        // 关键字查找
        input("keyword")        == null     || $where["begin_place|end_place|phone"] = ['like','%'.trim(input("keyword",'')).'%'];

        // 开始时间 结束时间
        if($s_time>0 && $e_time>0){
            $where['depart_time'] = ['between',[$s_time,$e_time]];

        // 开始时间
        }elseif($s_time>0){
            $where['depart_time'] = ['egt',$s_time];
        // 开始时间
        }elseif($e_time>0){
            $where['depart_time'] = ['lt',$e_time+24*60*60];
        }

        input("type")           == null     || $where['type'] = input("type");

        $res = Db::name('dd_publish')->page($page, $limit)
            ->where($where)
            ->select();
        foreach ($res as $index => $re) {
            $res[$index]['create_time'] = date('Y-m-d H:i:s', $re['create_time']);
            $res[$index]['depart_time'] = date('Y-m-d H:i:s', $re['depart_time']);
           // json对象到时要通过这个地方进行转换
        }

        // 根据关键字返回 相应数据
        $total = Db::name('dd_publish')
            -> where($where)
            ->count();
        return json(['code' => 1, 'msg' => 'ok', 'total' => $total, 'data' => $res]);
    }






}